import {
  BadRequestException,
  Controller,
  Get,
  Inject,
  Query,
} from '@nestjs/common'

import { AppService } from './app.service'
import { RedisService } from './redis/redis.service'

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Inject(RedisService)
  private readonly redisService: RedisService

  @Get('addPositions')
  async addPositions(
    @Query('positionName') positionName: string,
    @Query('longitude') longitude: number,
    @Query('latitude') latitude: number,
  ) {
    if (!positionName || !longitude || !latitude) {
      throw new BadRequestException('参数不全')
    }

    try {
      await this.redisService.addPositions('positions', positionName, [
        longitude,
        latitude,
      ])
    } catch (error) {
      throw new BadRequestException({ message: '添加失败' })
    }

    return { statusCode: 200, message: '添加成功' }
  }

  @Get('getPosition')
  async getPosition(@Query('positionName') positionName: string) {
    if (!positionName) {
      throw new BadRequestException('参数不全')
    }
  }

  @Get('getPositionList')
  async getPositionList() {
    const res = await this.redisService.getPositionList('positions')
    return res
  }

  @Get('getNearbyPositions')
  async getNearbyPositions(
    @Query('longitude') longitude: number,
    @Query('latitude') latitude: number,
    @Query('radius') radius: number,
  ) {
    if (!longitude || !latitude || !radius) {
      throw new BadRequestException('参数不全')
    }

    return await this.redisService.getNearbyPositions(
      'positions',
      [longitude, latitude],
      radius,
    )
  }
}
